
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        AddImageBlankAlts()
        
        EntityController.JavaScriptAddChildDiv(Me.DashboardDiv.ClientID, 30)
        EntityController.JavaScriptAddChildDiv(Me.QuickLinksDiv.ClientID, 30)
        EntityController.JavaScriptSetFixedHeight(0)
        Dim roleId As Integer = UserAuthorization.CurrentUser.RoleId
        If Not Me.IsPostBack Then
            If UserAuthorization.CurrentUser.IsSingleParticipantUser = True Then
                SetParticipantHomePageLayout()
            End If
            'If UserAuthorization.CurrentUser.RoleId = Cdm.DataCore.UserSecurity.Role.RoleType.SysAdmin Then
            'Response.Redirect("admin/users.aspx")
            'End If
            'Me.NameLabel.Text = GetUserDisplayName()
            'Me.w = String.Concat("Welcome to ", Application("AppName"), ": ")
            PopulateQuickLinks(roleId)
        Else
            If Request("HiddenScrollPositionValue") IsNot Nothing Then
                'Me.Page.Form.Attributes.Add("onload", "getScroll('" + Me.DashboardDiv.ClientID + "'," + Request("HiddenScrollPositionValue") + ");")
                ClientScript.RegisterStartupScript(GetType(Page), "startup", "<script type=""text/javascript"" language=""javascript""> getScroll('" + Me.DashboardDiv.ClientID + "'," + Request("HiddenScrollPositionValue") + "); </script>")

            End If
        End If
        ClientScript.RegisterOnSubmitStatement(GetType(Page), "frmsubmit", "setScrollById('" + Me.DashboardDiv.ClientID + "');")
        PopulateDashboard(roleId)
    End Sub

    Private Sub AddImageBlankAlts()
        Me.LeftCornerImage.Attributes.Add("alt", "")
        Me.Img1.Attributes.Add("alt", "")
        Me.Img2.Attributes.Add("alt", "")
        Me.Img3.Attributes.Add("alt", "")
        Me.Img4.Attributes.Add("alt", "")
        Me.RightCornerImage.Attributes.Add("alt", "")

    End Sub

    Private Sub SetParticipantHomePageLayout()
        'Me.QuickLinksDiv.Style("top") = "220px"
        'Me.QuickLinksDiv.Style("width") = "279px"
        Me.LeftCornerImage.Style("top") = "-15px"

        Me.RightCornerImage.Style("top") = "-15px"
        Me.RightCornerImage.Style("left") = "258px"
        Me.QuickLinksSpan.Style("font-size") = "1.5em"
        'Me.WelcomeDiv.Style("visibility") = "visible"
        SetWelcomeLiteral()
        'Me.QuickLinksItemsDiv.Style("top") = "278px"
        'Me.QuickLinksItemsDiv.Style("width") = "279px"

        'Me.GradientImage.Style("width") = "279px"

        'Me.QuickLinksItemsSpan.Style("left") = "28px"
        'Me.QuickLinksItemsSpan.Style("top") = "10px"
        Me.DashboardDiv.Style("left") = "345px"
        Me.DashboardDiv.Style("top") = "220px"
        'Me.DashboardDiv.Style("width") = "620px"
        Me.DashboardDiv.Style("height") = "287px"

        Me.trQuickSearch1.Visible = False
        Me.trQuickSearch2.Visible = False
        Me.trQuickSearch3.Visible = False
        Me.trQuickSearchTop.Visible = False
    End Sub
    ''' <summary>
    ''' Write a Table to Welcome the user
    ''' This is called for Participants, Reps, and Legal Guardians
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetWelcomeLiteral()
        Dim welcomeStringBuilder As New StringBuilder()
        welcomeStringBuilder.Append("<table cellpadding=""0"" cellspacing=""0"" ")
        welcomeStringBuilder.Append("style=""visibility:collapse; height:0px;"" border=""0"">")
        welcomeStringBuilder.Append("<tr style=""height:30px"" valign=""bottom""><td style=""width:20px;""></td>")
        welcomeStringBuilder.Append("<td style=""width:945px;""><div id=""WelcomeDiv"" runat=""server"">")
        welcomeStringBuilder.Append("<span style=""font-family: Arial, MS Sans Serif, Verdana, Sans-Serif; font-weight:bolder; font-size: 1em; color:#21334B"">")
        welcomeStringBuilder.Append("Welcome to ")
        welcomeStringBuilder.Append(Application("AppName"))
        welcomeStringBuilder.Append(": </span>")
        welcomeStringBuilder.Append("<span style=""font-family: Arial, MS Sans Serif, Verdana, Sans-Serif; font-weight:bolder; font-size: 1em; color:#A04111"">")
        welcomeStringBuilder.Append(GetUserDisplayName())
        welcomeStringBuilder.Append("</span></div></td></tr></table>")
        Me.WelcomeLiteral.Text = welcomeStringBuilder.ToString()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Form.DefaultButton = Me.QuickSearchButton.UniqueID
    End Sub


    Public Function GetDisplayName(ByVal inputValue As String) As String
        Return EntityController.ReplaceDisplayName(inputValue)
    End Function

    Private Function GetUserDisplayName() As String
        Dim usr As Cdm.DataCore.UserSecurity.CDMUser = UserAuthorization.CurrentUser
        Dim displayName As String = usr.FirstName
        If usr.MiddleName.Length > 0 Then
            displayName = String.Concat(displayName, " ", usr.MiddleName.Substring(0, 1).ToUpper, ".")
        End If
        displayName = String.Concat(displayName, " ", usr.LastName)
        Return displayName
    End Function

    Private Sub PopulateQuickLinks(ByVal roleId As Integer)

        Me.QuickLinksRepeater.DataSource = Cdm.DataCore.Navigation.NavigateQuickLinks.ListByUser(UserAuthorization.CurrentUser.Id)
        Me.QuickLinksRepeater.DataBind()
        Dim quickLinksSize As Integer = 25 * QuickLinksRepeater.Items.Count
        'Me.QuickLinksItemsDiv.Style("height") = quickLinksSize.ToString() + "px"
        'Me.GradientImage.Style("height") = quickLinksSize.ToString() + "px"
    End Sub


    Private Sub PopulateDashboard(ByVal roleId As Integer)

        Me.DashBoardAccordion.DataSource = Cdm.DataCore.Navigation.Dashboard.ListByRole(roleId)
        Me.DashBoardAccordion.DataBind()
    End Sub



    Protected Sub QuickSearchButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles QuickSearchButton.Click
        PerformQuickSearch()
    End Sub

    Private Sub PerformQuickSearch()
        Dim participantString = GetQuickSearchParticipantId()
        If IsNumeric(participantString) Then
            Response.Redirect(String.Concat("Case/EditFile.aspx?participantId=", participantString))
        Else
            If QuickSearchTextBox.Text.Trim().Length > 0 Then
                Response.Redirect("~/Search/AdvancedSearch.aspx?qs=" + Server.UrlEncode(QuickSearchTextBox.Text.Trim()))
            End If
        End If
    End Sub

    Private Function GetQuickSearchParticipantId() As String
        Try
            Dim parenStart As Integer = QuickSearchTextBox.Text.LastIndexOf("(")
            Dim parenEnd As Integer = QuickSearchTextBox.Text.LastIndexOf(")")
            If parenEnd = -1 Or parenStart = -1 Then
                Return ""
            End If

            If parenEnd < parenStart Then Return ""

            Return QuickSearchTextBox.Text.Substring(parenStart + 1, parenEnd - parenStart - 1)
        Catch
            Return ""
        End Try
    End Function

    Protected Sub DashBoardAccordion_ItemDataBound(ByVal sender As Object, ByVal e As AjaxControlToolkit.AccordionItemEventArgs) Handles DashBoardAccordion.ItemDataBound
        Dim DashboardPanel As Panel = e.AccordionItem.FindControl("DashboardItemPanel")

        If Not DashboardPanel Is Nothing Then
            Dim DashBoardDataItem As Cdm.DataCore.Navigation.Dashboard
            DashBoardDataItem = CType(e.AccordionItem.DataItem, Cdm.DataCore.Navigation.Dashboard)
            Dim ctl As Control = Me.LoadControl("DashboardControls/" & DashBoardDataItem.ControlName)
            Dim dash As IDashBoard = ctl
            dash.Title = EntityController.ReplaceDisplayName(DashBoardDataItem.Title)
            DashboardPanel.Controls.Add(ctl)
        End If
    End Sub

    Protected Sub QuickSearchTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles QuickSearchTextBox.TextChanged
        PerformQuickSearch()
    End Sub
End Class
